package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.injector.methods.UpdateById;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService{


    /**
     * 修改
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        //1、校验参数
          //判断频道名称和频道id是否为空
//        if (adChannel==null || StringUtils.isBlank(adChannel.getName()) ||adChannel.getId() == null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
//        }



          //先查再改
        AdChannel channel = getById(adChannel.getId());
          //判断查询是否为空
        if(channel == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }


        //判断原来是否有默认频道
        if(adChannel.getIsDefault()){

            //把原有的默认频道改为非默认
            LambdaQueryWrapper<AdChannel> query = new LambdaQueryWrapper<>();
            query.eq(AdChannel::getIsDefault,1);
            AdChannel one = getOne(query);

            //判断查询是否有默认频道
            if (one !=null){
                one.setIsDefault(false);
                updateById(one);
            }
        }

        //2、执行修改
        updateById(adChannel);


        //3、返回结果


        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //判断id 是否为空
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }

        AdChannel channel = getById(id);
//        //判断频道是否存在
//        if (channel == null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }

        //判断频道是否是默认频道
        if (channel.getIsDefault()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ISDEFAULT_INVALID);
        }

        //判断频道是否是启用状态
        if (channel.getStatus()){
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ISSTATUS);
        }

        removeById(id);

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 新增
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        //1、校验参数
          //判断频道名称是否为空
//        if (StringUtils.isBlank(adChannel.getName())){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
//        }

          //判断默认频道是否为空
//        if (adChannel.getIsDefault() == null){
//            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
//        }

        adChannel.setCreatedTime(new Date());

          //判断原来是否有默认频道
        if(adChannel.getIsDefault()){
            //把原有的默认频道改为非默认
            LambdaQueryWrapper<AdChannel> query = new LambdaQueryWrapper<>();
            query.eq(AdChannel::getIsDefault,1);
            AdChannel one = getOne(query);

            //判断查询是否有默认频道
            if (one !=null){
                one.setIsDefault(false);
                updateById(one);
            }
        }


        //2、执行新增
        save(adChannel);



        //3、返回结果

        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据名称分页查询频道列表信息
     *
     * @param channelDto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto channelDto) {

        //1、校验参数
        if(channelDto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
          //检查分页参数
        channelDto.checkParam();
        //2、分页查询
        IPage<AdChannel> pageParam = new Page<>(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<AdChannel> query = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(channelDto.getName())){
            query.like(AdChannel::getName,channelDto.getName());
        }
        query.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> pageResult = page(pageParam, query);

        ResponseResult result = new  PageResponseResult(channelDto.getPage(),channelDto.getSize(),pageResult.getTotal(),pageResult.getRecords());


        //3、返回结果

        return result;
    }
}
